package com.lkd.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lkd.dto.PolicyDTO;
import com.lkd.entity.Policy;
import com.lkd.entity.VM;
import com.lkd.result.TableInfo;
import com.lkd.service.PolicyService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: Z.lx
 * @Date: 2025/8/23 14:24
 * @Description:
 **/
@RestController
@RequestMapping("/api/vm-service/policy")
@AllArgsConstructor
@Slf4j
public class PolicyController {
    private final PolicyService policyService;
    /**
     * 获取策略
     * @param innerCode
     * @return
     */
    @GetMapping("/vmPolicy/{innerCode}")
    public Policy getPolicy(@PathVariable("innerCode") String innerCode){
        log.info("获取策略:{}",innerCode);
        return policyService.getPolicyByInnerCode(innerCode);
    }

    /**
     * 删除策略
     * @param id
     * @return
     */
    @DeleteMapping("/{policyId}")
    public boolean deletePolicy(@PathVariable("policyId") String id){
        log.info("删除策略:{}",id);
        return policyService.deletePolicy(id);
    }

    /**
     * 取消策略
     * @param policyId,innerCode
     * @return
     */
    @PutMapping("/cancelPolicy/{innerCode}/{policyId}")
    public boolean cancelPolicy(@PathVariable("policyId") String policyId,@PathVariable("innerCode") String innerCode){
        log.info("取消策略:{},{}",policyId,innerCode);
        return policyService.cancelPolicy(policyId,innerCode);
    }

    /**
     * 新增策略
     * @param policy
     * @return
     */
    @PostMapping
    public Boolean add(@RequestBody Policy policy){
        log.info("新增策略{}",policy);
        return policyService.add(policy);
    }

    /**
    * @Description 根据策略搜索售货机
    * @Param []
    * @Date 2025/8/23 14:53
    **/
    @GetMapping("/vmList/{policyId}")
    public TableInfo<VM> vmList(@PathVariable Long policyId, PolicyDTO policyDTO){
        log.info("根据策略搜索售货机:");
        policyDTO.setPolicyId(policyId);
        return policyService.vmList(policyId,policyDTO);
    }

    /**
    * @Description 搜索策略
    * @Param [id]
    * @Date 2025/8/23 14:53
    **/
    @GetMapping("/search")
    public TableInfo<Policy> search(PolicyDTO policyDTO){
        log.info("搜索策略:");
        return policyService.searchPolicy(policyDTO);
    }
    /**
    * @Description 修改策略
    * @Param [policyId]
    * @Date 2025/8/23 14:53
    **/
    @PutMapping("/{policyId}")
    public boolean update(@PathVariable String policyId,@RequestBody PolicyDTO policyDTO){
        return policyService.update(Wrappers.<Policy>lambdaUpdate()
                .eq(Policy::getPolicyId,policyId)
                .set(Policy::getPolicyName,policyDTO.getPolicyName())
                .set(Policy::getDiscount,policyDTO.getDiscount())
                .set(Policy::getUpdateTime, LocalDateTime.now()));
    }
}
